﻿namespace Blaze.Editor.DevPanels
{
    using UnityEditor;

    /// <summary>
    /// 服务器设置的快捷方式。
    /// </summary>
    public class ServerSettingShortcut
    {
        /// <summary>
        /// 设置的名称。
        /// </summary>
        public string Name;

        /// <summary>
        /// 设置所对应的值。
        /// </summary>
        public string Value;

        public ServerSettingShortcut(string name, string value)
        {
            Name = name;
            Value = value;
        }

        /// <summary>
        /// 返回表示当前对象的字符串。
        /// </summary>
        /// <returns>
        /// 表示当前对象的字符串。
        /// </returns>
        public override string ToString()
        {
            return string.Format("{0} {1}", Name, Value);
        }
    }

    /// <summary>
    /// 用于绘制带自定义选项的<see cref="ServerSettingShortcut"/>。
    /// </summary>
    public class ServerSettingShortcutDrawer
    {
        private string CustomValue
        {
            get
            {
                if (mCustomValue == null)
                    mCustomValue = ProjectEditorPrefs.GetString(mFieldKey, string.Empty);
                return mCustomValue;
            }
            set
            {
                if (mCustomValue == value)
                    return;
                mCustomValue = value;
                ProjectEditorPrefs.SetString(mFieldKey, value);
            }
        }

        public ServerSettingShortcutDrawer(string fieldName, string fieldKey, ServerSettingShortcut[] shortcuts)
        {
            mFieldName = fieldName;
            mFieldKey = fieldKey;
            mShortcuts = shortcuts;
            mOptions = new string[mShortcuts.Length + 1];
            for (var i = 0; i < mShortcuts.Length; i++)
                mOptions[i] = mShortcuts[i].Name;
            mOptions[mOptions.Length - 1] = "自定义";

            mSelectedIndex = mShortcuts.Length;
            for (var i = 0; i < mShortcuts.Length; i++)
            {
                if (mShortcuts[i].Value == CustomValue)
                {
                    mSelectedIndex = i;
                    break;
                }
            }
        }

        public string Draw()
        {
            string ret;
            EditorGUILayout.BeginHorizontal();

            EditorGUILayout.PrefixLabel(mFieldName);
            mSelectedIndex = EditorGUILayout.Popup(mSelectedIndex, mOptions);
            if (mSelectedIndex == mShortcuts.Length)
            {
                CustomValue = EditorGUILayout.DelayedTextField(CustomValue);
                ret = CustomValue;
            }
            else
            {
                var shortcut = mShortcuts[mSelectedIndex];
                var input = EditorGUILayout.DelayedTextField(shortcut.Value);
                if (input != shortcut.Value)
                {
                    mSelectedIndex = mShortcuts.Length;
                    CustomValue = input;
                }
                ret = input;
            }

            EditorGUILayout.EndHorizontal();
            return ret;
        }

        private readonly ServerSettingShortcut[] mShortcuts;
        private readonly string[] mOptions;
        private int mSelectedIndex;
        private readonly string mFieldName;
        private readonly string mFieldKey;
        private string mCustomValue;
    }
}